package com.yq.tanhua.ui.friends


import com.fmt.mvi.learn.gobal.log
import com.yq.tanhua.net.BaseViewModel
import com.yq.tanhua.net.TanHuaApiService
import com.yq.tanhua.net.vo.Movment
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow

class FriendsViewModel : BaseViewModel() {

    //推荐动态
    val recomendMovmentUiState = MutableStateFlow(MovmentsData(false, null))

    //关注朋友的动态
    val friendsMovmentUiState = MutableStateFlow(MovmentsData(false, null))

    var currentPage = 1
    var currentPageFried = 1
    val pageSize = 10

    //获取推荐动态
    fun getRecommendMovments(isFresh: Boolean) {

        launch(
            block = {
                if (isFresh) {
                    currentPage = 1
                    changeRecommendUIDataStates(RequestDataStatus.RefreshIng)
                } else {
                    changeRecommendUIDataStates(RequestDataStatus.LoadMoreIng)
                }

                val movmentPageData =
                    TanHuaApiService.getRecommendMovment(currentPage, pageSize).fullData()
                var datas: List<Movment>? = recomendMovmentUiState.value.datas
                val resultDatas = movmentPageData.items
                "获取到第${currentPage}页数据，个数是：${resultDatas.size}".log()

                if (resultDatas.isEmpty()) {
                    changeRecommendUIDataStates(if (isFresh) RequestDataStatus.NoData else RequestDataStatus.NoMoreData)
                } else {
                    if (isFresh) {
                        datas = resultDatas
                    } else {
                        (datas as ArrayList<Movment>).addAll(resultDatas)
                    }
                    recomendMovmentUiState.value = recomendMovmentUiState.value.copy(
                        isLoadMoreData = !isFresh,
                        datas = datas,
                        status = if (isFresh) RequestDataStatus.RefreshComplete else RequestDataStatus.LoadMoreComplete
                    )
                    currentPage++
                }
            },
            error = {
                if (isFresh) {
                    currentPage = 1
                    changeRecommendUIDataStates(RequestDataStatus.RefreshFail)
                } else {
                    changeRecommendUIDataStates(RequestDataStatus.LoadMoreFail)
                }
            }
        )
    }

    //获取关注朋友动态
    fun getFriendsMovment(isFresh: Boolean) {
        launch(
            block = {
                if (isFresh) {
                    currentPageFried = 1
                    changeLoadDataStates(RequestDataStatus.RefreshIng)
                } else {
                    changeLoadDataStates(RequestDataStatus.LoadMoreIng)
                }
                val movmentPageData =
                    TanHuaApiService.getFriendsMovment(currentPageFried, pageSize).fullData()
                var datas: List<Movment>? = friendsMovmentUiState.value.datas
                val resultDatas = movmentPageData.items
                "获取到第${currentPageFried}页数据，个数是：${resultDatas.size}".log()

                if (resultDatas == null || resultDatas.isEmpty()) {
                    changeLoadDataStates(if (isFresh) RequestDataStatus.NoData else RequestDataStatus.NoMoreData)
                } else {
                    if (isFresh) {
                        datas = resultDatas
                    } else {
                        (datas as ArrayList<Movment>).addAll(resultDatas)
                    }
                    friendsMovmentUiState.value = friendsMovmentUiState.value.copy(
                        isLoadMoreData = !isFresh,
                        datas = datas,
                        status = if (isFresh) RequestDataStatus.RefreshComplete else RequestDataStatus.LoadMoreComplete
                    )
                    currentPageFried++
                }

            },
            error = {
                if (isFresh) {
                    currentPageFried = 1
                    changeLoadDataStates(RequestDataStatus.RefreshFail)
                } else {
                    changeLoadDataStates(RequestDataStatus.LoadMoreFail)
                }
            }
        )
    }

    fun changeLoadDataStates(requestDataStatus: RequestDataStatus) {
        friendsMovmentUiState.value =
            friendsMovmentUiState.value.copy(status = requestDataStatus)
    }

    fun changeRecommendUIDataStates(requestDataStatus: RequestDataStatus) {
        recomendMovmentUiState.value =
            recomendMovmentUiState.value.copy(status = requestDataStatus)
    }


}

data class MovmentsData(
    val isLoadMoreData: Boolean = false,
    val datas: List<Movment>?,
    val status: RequestDataStatus? = null
)

sealed class RequestDataStatus {
    object RefreshIng : RequestDataStatus()
    object RefreshFail : RequestDataStatus()
    object RefreshComplete : RequestDataStatus()

    object LoadMoreIng : RequestDataStatus()
    object LoadMoreFail : RequestDataStatus()
    object LoadMoreComplete : RequestDataStatus()
    object NoMoreData : RequestDataStatus()

    object NoData : RequestDataStatus()
}